package com.xiguaji.tiktok.task.blogger.event;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.xiguaji.boot.eventbus.Event;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.eventbus.IEventListener;
import com.xiguaji.tiktok.task.blogger.spider.BloggerClient;
import com.xiguaji.tiktok.task.blogger.spider.BloggerFollowingResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SaveBloggerFollowingEventListener implements IEventListener<SaveBloggerFollowingEventListener.SaveBloggerFollowingEvent> {
    @Autowired
    BloggerClient bloggerClient;

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;

    @Override
    @Subscribe
    @AllowConcurrentEvents
    public void handle(SaveBloggerFollowingEvent event) {
        if (CollectionUtils.isEmpty(event.uids)) return;

        event.uids.parallelStream().forEach(uid -> {
            try {
                run(event, uid);
            } catch (Exception ex) {
                log.error("SaveBloggerFollowings error. uid:" + uid);
            }
        });
    }

    private void run(SaveBloggerFollowingEvent event, long uid) {
        long min_time = 0;
        boolean isHasMore = true;
        BloggerFollowingResponse response = null;

        List<Long> result = new ArrayList<>();
        try {
            while (isHasMore) {
                response = bloggerClient.getFollowings(uid, min_time, 20);

                if (response == null || CollectionUtils.isEmpty(response.getFollowings())) break;

                min_time = response.getMin_time();
                isHasMore = response.isHas_more();

                var followings = response.getFollowings();
                List<Long> uids = followings.stream()
                        .map(user -> Long.parseLong(user.getUid()))
                        .collect(Collectors.toList());

                result.addAll(uids);
            }
        } catch (Exception ex) {
            //记录异常，继续进行接下去的操作
            log.error(event.getSource() + " uid:" + uid + " count:" + result.size(), ex);
        }

        if (result.size() > 0) {
            var saveBloggerEvent = new SaveBloggerEventListener.SaveBloggerEvent(event, result, false);
            eventBusTemplate.post(saveBloggerEvent);
        }
    }

    public static class SaveBloggerFollowingEvent extends Event {
        public SaveBloggerFollowingEvent(List<Long> uids) {
            this(null, uids);
        }

        public SaveBloggerFollowingEvent(Event event, List<Long> uids) {
            super(event, "SaveBloggerFollowing");
            this.uids = uids;
        }

        private List<Long> uids;
    }
}
